﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;

namespace SyncTcpServer
{
    public partial class Form1 : Form
    {
        private IPAddress localAddress;
        private const int port = 51888;
        private TcpListener tcpListener;
        private TcpClient tcpClient;
        private NetworkStream networkStream;
        private BinaryReader br;
        private BinaryWriter bw;
        private int sendCount = 1;
        private int receiveCount = 10;
        private delegate void ShwMsgforViewCallBack(string str);
        ShwMsgforViewCallBack shwMsgforViewCallBack;
        private delegate void ShwStatusInfoCallBack(string str);
        ShwStatusInfoCallBack shwStatusInfoCallBack;
        private delegate void ShwProgressProcCallBack(int process);
        ShwProgressProcCallBack shwProgressProcCallBack;
        private delegate void ResetMsgTxtCallBack();
        ResetMsgTxtCallBack resetMsgTxtCallBack;

        public Form1()
        {
            InitializeComponent();
            shwMsgforViewCallBack = new ShwMsgforViewCallBack(ShwMsgForView);
            shwStatusInfoCallBack = new ShwStatusInfoCallBack(ShwStatusInfo);
            shwProgressProcCallBack = new ShwProgressProcCallBack(ShwProgressProc);
            resetMsgTxtCallBack = new ResetMsgTxtCallBack(ResetMsgTxt);
            IPAddress[] listenIp = Dns.GetHostAddresses("");
            localAddress = listenIp[0];
            tbxSendCount.Text = sendCount.ToString();
            tbxReceiveCount.Text = receiveCount.ToString();
            toolStripProcessProc.Minimum = 0;
        }

        private void ShwMsgForView(string str)
        {
            lstbxMsgView.Items.Add(str);
            lstbxMsgView.TopIndex = lstbxMsgView.Items.Count - 1;
        }

        private void ShwStatusInfo(string str)
        {
            toolStripStatusInfo.Text = str;
        }

        private void ShwProgressProc(int process)
        {
            toolStripProcessProc.Value = process;
        }

        private void ResetMsgTxt()
        {
            tbxMsg.Text = string.Empty;
            tbxMsg.Focus();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {

        }

        private void btnReceive_Click(object sender, EventArgs e)
        {

        }

        private void btnSend_Click(object sender, EventArgs e)
        {

        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {

        }

        private void btnStop_Click(object sender, EventArgs e)
        {

        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            tcpListener = new TcpListener(localAddress, port);
            tcpListener.Start();
            toolStripProcessProc.Maximum = 100;
            toolStripProcessProc.Value = 0;
            Thread threadAccept = new Thread(new ThreadStart(AcceptClientConnect));
            threadAccept.Start();
        }

        private void AcceptClientConnect()
        {
            statusStripInfo.Invoke(shwStatusInfoCallBack, "[" + localAddress + ":" + port + "] Listening...");
            Thread.Sleep(1000);
            try
            {
                statusStripInfo.Invoke(shwStatusInfoCallBack, "Waiting...");
                statusStripInfo.Invoke(shwProgressProcCallBack,1);
                tcpClient = tcpListener.AcceptTcpClient();
                statusStripInfo.Invoke(shwProgressProcCallBack, 100);
                if (tcpClient != null)
                {
                    statusStripInfo.Invoke(shwStatusInfoCallBack, "Accept a connection.");
                    networkStream = tcpClient.GetStream();
                    br = new BinaryReader(networkStream);
                    bw = new BinaryWriter(networkStream);
                }
            }
            catch (Exception)
            {
                statusStripInfo.Invoke(shwStatusInfoCallBack, "Stop Listening.");
                Thread.Sleep(1000);
                statusStripInfo.Invoke(shwProgressProcCallBack, 0);
                statusStripInfo.Invoke(shwStatusInfoCallBack, "Listen Ready.");
            }
        }
    }
}
